<template>
  <!-- 模型列表-单个模型 -->
  <div class="ListComponent-wrapper">
    <searchData :search-data="searchOptions" @search="handleSearch" />
    <list-table ref="table" :columns="columns" :pageKeys="['pageNum', 'pageSize', 'total']" :request="loadData" :select-config="{ use: true, type: 'checkbox', rowKey: 'id' }" row-key="id">
      <template #prefix="data">
        <a-button type="primary" icon="pushpin" :disabled="!data.selectedRowKeys || data.selectedRowKeys.length === 0" @click="handleOpen(data)">打开模型</a-button>
        <a-button  type="primary" v-if="hideBuquBtn()" icon="cloud-upload" :disabled="!projectInfo.projectId" class="ml12" @click="handleActions(0)">上传模型</a-button>
        <a-button  type="primary" icon="pie-chart" v-if="hideBuquBtn()" class="ml12" :disabled="!data.selectedRowKeys || data.selectedRowKeys.length < 2" @click="handleActions(4, data)">组合模型</a-button>
        <a-popover placement="top">
          <template slot="content">
            <div>
              <a href="/static/resource/RevitSetup.exe" target="_parent">RevitSetup.exe</a>
            </div>
            <div>
              <a href="/static/resource/NwDemoAddin.exe" target="_parent">NwDemoAddin.exe</a>
            </div>
          </template>
          <a-button type="primary" class="ml12" icon="paper-clip"> 下载插件 </a-button>
        </a-popover>
        <!-- <a-button type="primary" icon="paper-clip" class="ml12"><a href="/static/resource/REVIT-INSTALL.zip" target="_parent" style="color: #fff; margin-left: 8px">下载插件</a></a-button> -->
      </template>
      <!-- 模型名称 -->
      <a slot="modelName" slot-scope="text, record" @click="handleOpen([{ modelId: record.modelFileId, version: record.modelFileVersion, modelTitle: record.modelName }], record)">{{ text }}</a>
      <!-- 版本 -->
      <a slot="modelVersion" slot-scope="text, record" @click.stop="handleActions(3, record)">{{ text }}</a>
      <!-- 操作 -->
      <template slot="actions" slot-scope="text, record">
        <MoreButton>
        <a class="base-btn" @click.stop="handleActions(2, record)" v-if="hideBuquBtn()" >更新模型</a>
        <a class="base-btn" @click.stop="handleActions(1, record)" v-if="hideBuquBtn()" >编辑</a>
        <a class="base-btn del_btn" @click.stop="deleteModel(record)" v-if="hideBuquBtn()" >删除</a>
        </MoreButton>
      </template>
    </list-table>
    <!-- 上传模型 -->
    <dialog-model ref="dialogForm" :visible.sync="visible" :edit-model="editModel"
                  :forms.sync="form" :refresh="handleSearch"
                  @updateActiveKey="$emit('update:activeKey', 'compose')"> </dialog-model>
  </div>
</template>

<script>
import { getPageTypesLabel } from '@/api/bimIntegrate/bimResource.js'
import Selector from '@/components/Select/Selector'
import { getList, deleteBimModel } from '@/api/bimIntegrate/bimModel'
import DialogModel from '@/views/bimView/modelList/components/DialogModel' // 弹窗
import { deleteModel } from '@/api/bimIntegrate'

const searchOptions = [
  {
    name: '模型名称',
    value: 'modelName',
    type: 'input'
  },
  {
    name: '模型分类',
    valueKey: 'modelClassifyId',
    type: Selector,
    customApi: getPageTypesLabel,
    customQuery: { param: { type: '38' } },
    dataValue: 'data.list'
  },
  {
    name: '模型类别',
    valueKey: 'modelTypeId',
    type: Selector,
    customApi: getPageTypesLabel,
    customQuery: { param: { type: '33' } },
    dataValue: 'data.list'
  },
  {
    name: '专业分类',
    valueKey: 'professionalTypeId',
    type: Selector,
    customApi: getPageTypesLabel,
    customQuery: { param: { type: '34' } },
    dataValue: 'data.list'
  },
  {
    name: '楼层标签',
    valueKey: 'floorLabelId',
    type: Selector,
    customApi: getPageTypesLabel,
    customQuery: { param: { type: '35' } },
    dataValue: 'data.list'
  },
  {
    name: '详细程度',
    valueKey: 'detailDegreeId',
    type: Selector,
    customApi: getPageTypesLabel,
    customQuery: { param: { type: '37' } },
    dataValue: 'data.list'
  },
  {
    name: '模型来源',
    valueKey: 'modelSourceId',
    type: Selector,
    customApi: getPageTypesLabel,
    customQuery: { param: { type: '36' } },
    dataValue: 'data.list'
  },
  {
    name: '修改人',
    value: 'modifiedUserName',
    type: 'input'
  },
  {
    name: '更新时间',
    value: 'updateTime',
    type: 'dateRange',
    attr: {
      rangeFormat: ['modifiedStartTime', 'modifiedEndTime']
    }
  }
]
const columns = [
  {
    title: '序号',
    width: 80,
    dataIndex: 'index',
    // fixed: 'left',
    customRender() {
      return arguments[2] + 1
    }
  },
  {
    title: '模型名称',
    // fixed: 'left',
    // width: 200,
    dataIndex: 'modelName',
    scopedSlots: { customRender: 'modelName' }
  },
  {
    title: '模型分类',
    dataIndex: 'modelClassifyName'
  },
  {
    title: '模型类别',
    dataIndex: 'modelTypeName'
  },
  {
    title: '专业分类',
    dataIndex: 'professionalTypeName'
  },
  {
    title: '楼层标签',
    dataIndex: 'floorLabelName'
  },
  {
    title: '模型介绍',
    dataIndex: 'modelIntroduce',
    ellipsis: true
  },
  {
    title: '版本说明',
    dataIndex: 'versionExplain',
    ellipsis: true
  },
  {
    title: '模型大小',
    dataIndex: 'modelSize',
    customRender(e) {
      return e ? e + 'M' : '无'
    }
  },
  {
    title: '更新时间',
    dataIndex: 'modifiedTime'
  },
  {
    title: '模型来源',
    dataIndex: 'modelSourceName',
    ellipsis: true
  },
  {
    title: '详细程度',
    dataIndex: 'detailDegreeName'
  },
  {
    title: '修改人',
    dataIndex: 'modifiedUserName'
  },
  {
    title: '版本',
    dataIndex: 'modelVersion',
    // fixed: 'right',
    scopedSlots: { customRender: 'modelVersion' }
  },
  {
    title: '操作',
    dataIndex: 'actions',
    fixed: 'right',
    scopedSlots: { customRender: 'actions' }
  }
]

export default {
  name: 'ListComponent',
  components: {
    DialogModel
  },
  data() {
    return {
      searchOptions,
      columns,
      visible: false, // 模型表单弹窗显隐控件
      form: {
        projectId: '',
        projectName: '',
        modelVersion: '',
        fileList: []
      }, //弹窗表单
      queryParams: {}, // 搜索条件参数
      editModel: 0 // 操作类型：0 上传模型；1 编辑；2 更新模型版本；3 历史版本；4 组合模型
    }
  },
  props: {
    // 项目信息
    projectInfo: Object
  },
  watch: {
    // 监听项目id刷新列表
    'projectInfo.projectId'(e) {
      e && this.$refs.table?.getData(null, true)
    }
  },
  methods: {
    /**
     * @description: 删除模型
     * @param {Object} e：行对象
     */
    deleteModel(e) {
      this.$confirm({
        title: '提示',
        content: '确认删除该模型？',
        onOk: () => {
          return deleteBimModel({
            id: e.id
          }).then(async (res) => {
            if (res.code === '0') {
              const [err, res] = await deleteModel({
                modelId: e.modelFileId
              })
              if (!err) {
                this.$refs.table.selectedRowKeys = []
                this.$refs.table.selectedRowsMap = new Map()
                this.$message.success('删除成功')
                this.handleSearch() // 刷新列表
              }
            }
          })
        }
      })
    },
    /**
     * @description: 打开模型
     * @param {Array} data：模型列表或者模型选中项
     */
    handleOpen(data, record) {
      let modelList = data.selectedRowsMap
        ? Array.from(data.selectedRowsMap, (item) => item[1])?.[0]?.map((i) => {
            return {
              modelId: i.modelFileId,
              version: i.modelFileVersion,
              modelTitle: i.modelName
            }
          })
        : data
      this.$router.push({
        path: '/bimIntegrate/model/modelPreview',
        query: {
          modelList: JSON.stringify(modelList),
          id: record.id,
          isModelList: 1
        }
      })
    },
    /**
     * @description: 上传模型
     * @param {Number} type：操作类型：0 新增；1 编辑；2 更新模型版本；3 历史版本；4 组合模型
     * @param {Object} record：操作数据对象
     */
    handleActions(type, record) {
      this.editModel = type
      if (this.editModel === 4) {
        let modelList = Array.from(record.selectedRowsMap, (item) => item[1])?.[0]
        let modelIds = modelList?.map((i) => i.id)
        let portfolioModelSize = modelList
          ?.map((item) => Number(item.modelSize))
          ?.reduce((pre, next) => pre + next)
          ?.toFixed(2)
        this.form = {
          modelIds,
          portfolioModelSize,
          projectId: this.projectInfo.projectId
        }
      } else {
        let form = record
          ? this.$clone(record)
          : {
              projectId: this.projectInfo.projectId,
              projectName: this.projectInfo.projectName,
              modelVersion: 'V.1',
              fileList: []
            }
        this.form = form
      }
      // 打开弹窗
      this.visible = true
    },
    /**
     * @description: 模型列表分页查询接口
     * @param {*} params:分页参数
     * @return {Promise}
     */
    loadData(params) {
      if (!this.projectInfo.projectId) {
        return Promise.resolve({ code: 200, total: 0, data: [] })
      }
      let args = Object.assign(params, { param: { ...this.queryParams, projectId: this.projectInfo.projectId } })
      // return Promise.resolve({ code: 200, data: [{ name: '我是模板' }] })
      return getList(args).then((res) => {
        if (res.code === '0') {
          return { code: 200, data: res.data?.list, total: res.data?.total }
        }
      })
    },
    /**
     * @description: 搜索
     * @param {*} val：搜索参数
     */
    handleSearch(val) {
      val && (this.queryParams = val)
      this.$refs.table.getData(null, true)
    }
  }
}
</script>

<style>
</style>
